package net.sk.china.city.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import net.sk.china.city.controller.base.BaseController;
import net.sk.china.city.domain.ArSupermarketProduct;
import net.sk.china.city.domain.dto.AjaxResult;
import net.sk.china.city.domain.dto.TableDataInfo;
import net.sk.china.city.domain.vo.AddSupermarketProductVO;
import net.sk.china.city.domain.vo.SupermarketProductVO;
import net.sk.china.city.domain.vo.UpdateSupermarketProductVO;
import net.sk.china.city.service.IArSupermarketProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 超市商品管理Controller
 * 
 * @author gxf'
 * @date 2024-03-27
 */
@Api(tags = "慈善超市")
@RequiredArgsConstructor
@RestController
@RequestMapping("/supermarket/product")
public class ArSupermarketProductController extends BaseController
{
    @Autowired
    private IArSupermarketProductService arSupermarketProductService;

    /**
     * 查询超市商品管理列表
     */
    @ApiOperation("商品列表")
    @PreAuthorize("hasAnyAuthority('supermarket:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArSupermarketProduct arSupermarketProduct)
    {
        startPage();
        List<SupermarketProductVO> list = arSupermarketProductService.selectArSupermarketProductList(arSupermarketProduct);
        return getDataTable(list);
    }

    /**
     * 导出超市商品管理列表
     */
//    @PreAuthorize("@ss.hasPermi('supermarket:product:export')")
//    @Log(title = "超市商品管理", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, ArSupermarketProduct arSupermarketProduct)
//    {
//        List<ArSupermarketProduct> list = arSupermarketProductService.selectArSupermarketProductList(arSupermarketProduct);
//        ExcelUtil<ArSupermarketProduct> util = new ExcelUtil<ArSupermarketProduct>(ArSupermarketProduct.class);
//        util.exportExcel(response, list, "超市商品管理数据");
//    }

    /**
     * 获取超市商品管理详细信息
     */
    @ApiOperation("商品详情")
    @PreAuthorize("hasAnyAuthority('supermarket:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(arSupermarketProductService.selectArSupermarketProductById(id));
    }

    /**
     * 新增超市商品管理
     */
    @ApiOperation("新增商品")
    @PreAuthorize("hasAnyAuthority('supermarket:product:add')")
    @PostMapping
    public AjaxResult add(@RequestBody AddSupermarketProductVO addSupermarketProductVO)
    {
        return toAjax(arSupermarketProductService.insertArSupermarketProduct(addSupermarketProductVO));
    }

    /**
     * 修改超市商品管理
     */
    @ApiOperation("修改商品")
    @PreAuthorize("hasAnyAuthority('supermarket:product:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody UpdateSupermarketProductVO updateSupermarketProductVO)
    {
        return toAjax(arSupermarketProductService.updateArSupermarketProduct(updateSupermarketProductVO));
    }

    /**
     * 删除超市商品管理
     */
    @ApiOperation("删除商品")
    @PreAuthorize("hasAnyAuthority('supermarket:product:remove')")
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long[] id)
    {
        return toAjax(arSupermarketProductService.deleteArSupermarketProductByIds(id));
    }


    @ApiOperation("上传商品图片")
    @PostMapping("/uploadProductImg")
    public AjaxResult uploadProductImg(@RequestPart("productImg") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
//            String avatar = FileUploadUtils.upload(RuoYiConfig.getProductPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
//            return success(avatar);
        }
        return error("上传图片异常，请联系管理员");
    }

    @ApiOperation("修改商品状态")
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody ArSupermarketProduct arSupermarketProduct)
    {
        return success(arSupermarketProductService.updateStatus(arSupermarketProduct));
    }

    @ApiOperation("删除库存信息")
    @GetMapping("/deleteInventory")
    public AjaxResult deleteInventory(Long inventoryId){
        return success(arSupermarketProductService.deleteInventory(inventoryId));
    }
}
